using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Text;
using ECommerce.DAL;


namespace Entity
{
	/// <summary>
	/// This object represents the Private Fields of TblAddressBook.
	/// </summary>
	
	[System.ComponentModel.DataObject]
    public class TblAddressBook : DatabaseUtility
    {
		#region TblAddressBook Private Fields


		Int32 _addressBookId;
		Int32 _userId;
		String _userFirstName = String.Empty;
		String _userLastName = String.Empty;
		String _userTelephone1 = String.Empty;
		String _userTelephone2 = String.Empty;
		String _userEmailAddress = String.Empty;
		String _userAddressLine1 = String.Empty;
		String _userAddressLine2 = String.Empty;
		String _companyName = String.Empty;
		String _userCity = String.Empty;
		String _userState = String.Empty;
		String _userZip = String.Empty;
		String _userCountry = String.Empty;
		Boolean _isDefault;
		
		#endregion
		#region TblAddressBook Constractor
		/// <summary>
        /// Creates an empty object of TblAddressBook. 
        /// This is mainly used to create a new instance of TblAddressBook to add a new TblAddressBook.
        /// </summary>
		public TblAddressBook(){}
		
				
		/// <summary>
        /// Used to retrieve employee information from database, 
        /// which is also used by Update method to save information of an existing TblAddressBook.
        /// </summary>
		public TblAddressBook
		(
			Int32 addressBookId,
			Int32 userId,
			String userFirstName,
			String userLastName,
			String userTelephone1,
			String userTelephone2,
			String userEmailAddress,
			String userAddressLine1,
			String userAddressLine2,
			String companyName,
			String userCity,
			String userState,
			String userZip,
			String userCountry,
			Boolean isDefault
		)
		{
			_addressBookId = addressBookId;	
			_userId = userId;	
			_userFirstName = userFirstName;	
			_userLastName = userLastName;	
			_userTelephone1 = userTelephone1;	
			_userTelephone2 = userTelephone2;	
			_userEmailAddress = userEmailAddress;	
			_userAddressLine1 = userAddressLine1;	
			_userAddressLine2 = userAddressLine2;	
			_companyName = companyName;	
			_userCity = userCity;	
			_userState = userState;	
			_userZip = userZip;	
			_userCountry = userCountry;	
			_isDefault = isDefault;	
		}
		
		#endregion
		#region TblAddressBook Properties
		
		 /***********************************************************************************************
                                    -- PROPERTIES --        
         Contains all the properties representing the data fields of the current entity.
         ***********************************************************************************************/

        /// <summary>
        /// This is the Primary key value of the AddressBookId, which can't be assigned from the application,
        /// to ensure the data consistancy, by not letting the user to change the PK_TblAddressBook to update the data of an another TblAddressBook instance.
        /// For view or updation purpose, every TblAddressBook will be retrieved thru the proper 'Get' method.
        /// </summary>
		
		public Int32 AddressBookId
		{
			get {return _addressBookId;}	
		}
		
				
		public Int32 UserId
		{
			get {return _userId;}
			set {_userId = value;}
		}

		public String UserFirstName
		{
			get {return _userFirstName;}
			set {_userFirstName = value;}
		}

		public String UserLastName
		{
			get {return _userLastName;}
			set {_userLastName = value;}
		}

		public String UserTelephone1
		{
			get {return _userTelephone1;}
			set {_userTelephone1 = value;}
		}

		public String UserTelephone2
		{
			get {return _userTelephone2;}
			set {_userTelephone2 = value;}
		}

		public String UserEmailAddress
		{
			get {return _userEmailAddress;}
			set {_userEmailAddress = value;}
		}

		public String UserAddressLine1
		{
			get {return _userAddressLine1;}
			set {_userAddressLine1 = value;}
		}

		public String UserAddressLine2
		{
			get {return _userAddressLine2;}
			set {_userAddressLine2 = value;}
		}

		public String CompanyName
		{
			get {return _companyName;}
			set {_companyName = value;}
		}

		public String UserCity
		{
			get {return _userCity;}
			set {_userCity = value;}
		}

		public String UserState
		{
			get {return _userState;}
			set {_userState = value;}
		}

		public String UserZip
		{
			get {return _userZip;}
			set {_userZip = value;}
		}

		public String UserCountry
		{
			get {return _userCountry;}
			set {_userCountry = value;}
		}

		public Boolean IsDefault
		{
			get {return _isDefault;}
			set {_isDefault = value;}
		}
		

		#endregion
		#region TblAddressBook Static Methods
		/************************************************************************************
                                    -- CREATE METHOD (STATIC) --
        *************************************************************************************/
		/************************* Architecure note:**********************************
        Note 1:
        This method is specially useful for the case, where we need to use Object Data Source controls.
        For ObjectDataSource/SqlDataSource controls, the insert/update metnod requires paramters methods, rather property based methods.
        However, there is still a way to pass property based objects as method parameter for data source controls, 
        but in that case we could not use Sql type, as view controls or asp parameter control can only contain few of primitive type.
        The main reason to remain the Sql Type in our object architecture is it is faster and contains better way for null value support.
        
        Note 2: 
        Non-primary key: it should be primitive type, as the asp paramter control (included in data source control) supports only primitive types.
        /*****************************************************************************/
		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public static  Int32 CreateNewTblAddressBook
		(
			Int32 userId,
			String userFirstName,
			String userLastName,
			String userTelephone1,
			String userTelephone2,
			String userEmailAddress,
			String userAddressLine1,
			String userAddressLine2,
			String companyName,
			String userCity,
			String userState,
			String userZip,
			String userCountry,
			Boolean isDefault
		)
		{
			//creating an empty TblAddressBook object
            TblAddressBook tblAddressBook = new TblAddressBook();
				tblAddressBook.UserId = userId;
				tblAddressBook.UserFirstName = userFirstName;
				tblAddressBook.UserLastName = userLastName;
				tblAddressBook.UserTelephone1 = userTelephone1;
				tblAddressBook.UserTelephone2 = userTelephone2;
				tblAddressBook.UserEmailAddress = userEmailAddress;
				tblAddressBook.UserAddressLine1 = userAddressLine1;
				tblAddressBook.UserAddressLine2 = userAddressLine2;
				tblAddressBook.CompanyName = companyName;
				tblAddressBook.UserCity = userCity;
				tblAddressBook.UserState = userState;
				tblAddressBook.UserZip = userZip;
				tblAddressBook.UserCountry = userCountry;
				tblAddressBook.IsDefault = isDefault;
			
			return TblAddressBook.CreateNewTblAddressBook(tblAddressBook);
		}
	
		/************************************************************************************
                 -- UPDATE METHODS (STATIC) --
        *************************************************************************************/

        /************************* Architecure note:**********************************
        Note 1:
        This method is specially useful for the case, where we need to use Object Data Source controls.
        For ObjectDataSource/SqlDataSource controls, the insert/update metnod requires paramters methods, rather property based methods.
        However, there is still a way to pass property based objects as method parameter for data source controls, 
        but in that case we could not use Sql type, as view controls or asp parameter control can only contain few of primitive type.
        The main reason to remain the Sql Type in our object architecture is it is faster and contains better way for null value support.
        
        Note 2: 
        There the two types of paramaters will be passed here.
         * a. Primary key paramater: it should be Sql type, as we will bind the primary key property for the view controls (form view, details view etc) as datakey.
         * b. Non-primary key: it should be primitive type, as the asp paramter control (included in data source control) supports only primitive types.
        /*****************************************************************************/
		
		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static  bool UpdateTblAddressBook
		(
			Int32 addressBookId,
			Int32 userId,
			String userFirstName,
			String userLastName,
			String userTelephone1,
			String userTelephone2,
			String userEmailAddress,
			String userAddressLine1,
			String userAddressLine2,
			String companyName,
			String userCity,
			String userState,
			String userZip,
			String userCountry,
			Boolean isDefault
		)
		{
			 TblAddressBook tblAddressBook =  TblAddressBook.GetTblAddressBookByAddressBookId(addressBookId);
			
				tblAddressBook.UserId = userId;
				tblAddressBook.UserFirstName = userFirstName;
				tblAddressBook.UserLastName = userLastName;
				tblAddressBook.UserTelephone1 = userTelephone1;
				tblAddressBook.UserTelephone2 = userTelephone2;
				tblAddressBook.UserEmailAddress = userEmailAddress;
				tblAddressBook.UserAddressLine1 = userAddressLine1;
				tblAddressBook.UserAddressLine2 = userAddressLine2;
				tblAddressBook.CompanyName = companyName;
				tblAddressBook.UserCity = userCity;
				tblAddressBook.UserState = userState;
				tblAddressBook.UserZip = userZip;
				tblAddressBook.UserCountry = userCountry;
				tblAddressBook.IsDefault = isDefault;
			
			return TblAddressBook.UpdateTblAddressBook(tblAddressBook);
		}

		/************************************************************************************
                         -- DELETE METHODS --
        *************************************************************************************/
        /************************* Architecure note:**********************************
        We have used the employeeId parameter as SqlInt32, to bind the delete method with object data source and view control directly.
        ****************************************************************************/
		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static  bool DeleteTblAddressBook
		(
			Int32 addressBookId
		)
		{
			//Validate Input
			if(addressBookId <= GetAddressBookIdMinValue)
				throw (new ArgumentOutOfRangeException("addressBookId"));
				
			// Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
			AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
			AddParameterToSqlCmd(sqlCmd, "@AddressBookId",SqlDbType.Int, 0, ParameterDirection.Input, addressBookId);
			SetCommandType(sqlCmd,CommandType.StoredProcedure,SPR_TBLADDRESSBOOK_DELETETBLADDRESSBOOK);
			ExecuteScalarCmd(sqlCmd);
			int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
			return (returnValue == 0 ? false : true);
			
		}
		
		/// <summary>
        /// Deletes a set of employees from the database.
        /// </summary>
		public static bool DeleteTblAddressBookList( System.Collections.Generic.List<Int32>  addressBookIdListToDelete)
		{
			//Validate Input
			foreach (Int32 addressBookId in addressBookIdListToDelete)
			    if(addressBookId <= GetAddressBookIdMinValue)
					throw (new ArgumentOutOfRangeException("addressBookId"));
			
			//performing deletion operation //

            string xmlString = FormatXMLForIdArray(addressBookIdListToDelete);
			
			// Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
			AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@AddressBookIdList", SqlDbType.Xml, xmlString.ToString().Length, ParameterDirection.Input, xmlString.ToString());
			SetCommandType(sqlCmd,CommandType.StoredProcedure,SPR_TBLADDRESSBOOK_DELETETBLADDRESSBOOKLIST);
			ExecuteScalarCmd(sqlCmd);
			int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
			return (returnValue == addressBookIdListToDelete.Count ? true : false);
		}
		
		/************************************************************************************
                                 -- GET BY PRIMARY METHODS --
        *************************************************************************************/
        /************************* Architecure note:**********************************
        Retrives an Employee from database, based on it's primary key.
        To view or update an employee, everytime we must have to use this type of 'get' method, 
        since we don't have direct access to the Primary key value for assigning value.
        We have used the employeeId parameter as SqlInt32, to bind the delete method with object data source and view control directly.
        /*****************************************************************************/
		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
		public static TblAddressBook GetTblAddressBookByAddressBookId(Int32 addressBookId)
		{
			if(addressBookId <= GetAddressBookIdMinValue)
					throw (new ArgumentOutOfRangeException("addressBookId"));
				
		 	// Execute SQL Command
			SqlCommand sqlCmd = new SqlCommand();
			AddParameterToSqlCmd(sqlCmd, "@AddressBookId",SqlDbType.Int, 0, ParameterDirection.Input, addressBookId);
			SetCommandType(sqlCmd,CommandType.StoredProcedure,SPR_TBLADDRESSBOOK_GETTBLADDRESSBOOKBYADDRESSBOOKID);
			GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateTblAddressBookCollectionFromReader);
            CustomCollection<TblAddressBook> objCollection = ((CustomCollection<TblAddressBook>)ExecuteReaderCmd(sqlCmd, test));

            if (objCollection.Count > 0)
                return objCollection[0];
            else
                return null;
		}
		
		/************************************************************************************
                         -- GET BY FOREIGN KEY METHODS --
        *************************************************************************************/
				
		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
		public static CustomCollection<TblAddressBook> GetTblAddressBookByUserId(Int32 userId)
		{
			if(userId <= GetUserIdMinValue)
					throw (new ArgumentOutOfRangeException("userId"));
				
		 	// Execute SQL Command
			SqlCommand sqlCmd = new SqlCommand();
			AddParameterToSqlCmd(sqlCmd, "@UserId",SqlDbType.Int, 0, ParameterDirection.Input, userId);
			SetCommandType(sqlCmd,CommandType.StoredProcedure,SPR_TBLADDRESSBOOK_GETTBLADDRESSBOOKBYUSERID);
			GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateTblAddressBookCollectionFromReader);
            CustomCollection<TblAddressBook> objCollection = ((CustomCollection<TblAddressBook>)ExecuteReaderCmd(sqlCmd, test));

            if (objCollection.Count > 0)
                return objCollection;
            else
                return null;
		}
		
		/// <summary>
        /// Gets all the TblAddressBook regarding 'PREFERRED_LANGUAGE_ID' foreign key.
        /// It supports efficient paging.
        /// </summary>
        /// <returns>A collection of TblAddressBook.</returns>
		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
        public static CustomCollection<TblAddressBook> GetTblAddressBookByUserIdPaged(Int32 userId,String orderBy, Int32 startRowIndex, Int32 maximumRows)
		{
            //Validate Input
			if(userId <= GetUserIdMinValue)
					throw (new ArgumentOutOfRangeException("userId"));
			
            if (orderBy == "")
                orderBy = "UserId";

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
			AddParameterToSqlCmd(sqlCmd, "@UserId",SqlDbType.Int, 0, ParameterDirection.Input, userId);
            AddParameterToSqlCmd(sqlCmd, "@orderby", SqlDbType.VarChar, 50, ParameterDirection.Input, orderBy);
            AddParameterToSqlCmd(sqlCmd, "@startrow", SqlDbType.Int, 0, ParameterDirection.Input, startRowIndex);
            AddParameterToSqlCmd(sqlCmd, "@pagesize", SqlDbType.Int, 0, ParameterDirection.Input, maximumRows);

            SetCommandType(sqlCmd,CommandType.StoredProcedure,SPR_TBLADDRESSBOOK_GETTBLADDRESSBOOKBYUSERIDPAGED);

            GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateTblAddressBookCollectionFromReader);
            CustomCollection<TblAddressBook> objCollection = ((CustomCollection<TblAddressBook>)ExecuteReaderCmd(sqlCmd, test));

            return objCollection;
        }
		
		/// <summary>
        /// This is a supporting method of 'GetEmployeeByPREFERRED_LANGUAGE_IDPagedPagedCountPaged' method.
        /// Used in paging, which returns the total number of records, returned from the ''GetEmployeeByReportsToPaged'' method (regardless of page size).
        /// Although we are not using the parameters, but we need the same signature as ''GetEmployeeByReportsToPaged'' to support paging.
        /// </summary>
		public static int GetTblAddressBookByUserIdPagedCount(Int32 userId,String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            //Validate Input
			if(userId <= GetUserIdMinValue)
					throw (new ArgumentOutOfRangeException("userId"));
			
			// Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
			AddParameterToSqlCmd(sqlCmd, "@UserId",SqlDbType.Int, 0, ParameterDirection.Input, userId);
			AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(sqlCmd,CommandType.StoredProcedure,SPR_TBLADDRESSBOOK_GETTBLADDRESSBOOKBYUSERIDPAGEDCOUNT);
            ExecuteScalarCmd(sqlCmd);

            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return returnValue;
        }
		
		/************************************************************************************
                 -- GET ALL METHODS --
        *************************************************************************************/
		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<TblAddressBook> GetAllTblAddressBook()
		{
		
		 	// Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd,CommandType.StoredProcedure,SPR_TBLADDRESSBOOK_GETALLTBLADDRESSBOOK);
            GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateTblAddressBookCollectionFromReader);
            CustomCollection<TblAddressBook> objCollection = ((CustomCollection<TblAddressBook>)ExecuteReaderCmd(sqlCmd, test));

            return objCollection;
		}
		
		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<TblAddressBook> GetAllTblAddressBookPaged(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            //Validate Input
            if (orderBy == "")
                orderBy = "AddressBookId";

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@orderby", SqlDbType.VarChar, 50, ParameterDirection.Input, orderBy);
            AddParameterToSqlCmd(sqlCmd, "@startrow", SqlDbType.Int, 0, ParameterDirection.Input, startRowIndex);
            AddParameterToSqlCmd(sqlCmd, "@pagesize", SqlDbType.Int, 0, ParameterDirection.Input, maximumRows);

            SetCommandType(sqlCmd,CommandType.StoredProcedure,SPR_TBLADDRESSBOOK_GETALLTBLADDRESSBOOKPAGED);

            GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateTblAddressBookCollectionFromReader);
            CustomCollection<TblAddressBook> objCollection = ((CustomCollection<TblAddressBook>)ExecuteReaderCmd(sqlCmd, test));
            return objCollection;
        }
		
		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static int GetAllTblAddressBookPagedCount(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();

            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(sqlCmd,CommandType.StoredProcedure,SPR_TBLADDRESSBOOK_GETALLTBLADDRESSBOOKPAGEDCOUNT);
            ExecuteScalarCmd(sqlCmd);

            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return returnValue;
        }
		
		#endregion
		#region Employee Instance Methods
		/************************************************************************************
                                    -- SAVE (INSERT/UPDATE) METHODS (INSTANCE) --
        *************************************************************************************/
		/// <summary>
        /// Saves (inserts and updates) an Employee into database.
        /// The insertion occur if the EmployeeId conatins default value.
        /// Otherwises it updates the Employee into the database, based on the primary key value stored in EmployeeId.
        /// This technique ensures an Employee will not be updated without having a valid EmployeeId.
        /// </summary>
		public bool Save()
		{
			if(AddressBookId<= GetAddressBookIdMinValue)
			{
				Int32 TempId=CreateNewTblAddressBook(this);
				if(TempId>0)
				{
					_addressBookId=TempId;
					return true;
				}
				else
				{
					// error occurs
					return false;
				}
			}
			else
			{	//This is a update Command
				return (UpdateTblAddressBook(this));
			}
			
		}
		
		public static Int32 CreateNewTblAddressBook(TblAddressBook newTblAddressBook)
		{
			// validate Parameters
			if (newTblAddressBook == null)
				throw (new ArgumentNullException("newTblAddressBook"));

			// Execute SQL Command
			SqlCommand sqlCmd = new SqlCommand();
			int length = 0;
			AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            
			AddParameterToSqlCmd(sqlCmd, "@UserId",SqlDbType.Int, 0, ParameterDirection.Input, newTblAddressBook.UserId);
			AddParameterToSqlCmd(sqlCmd, "@UserFirstName",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserFirstName);
			AddParameterToSqlCmd(sqlCmd, "@UserLastName",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserLastName);
			AddParameterToSqlCmd(sqlCmd, "@UserTelephone1",SqlDbType.NVarChar, 50, ParameterDirection.Input, newTblAddressBook.UserTelephone1);
			AddParameterToSqlCmd(sqlCmd, "@UserTelephone2",SqlDbType.NVarChar, 50, ParameterDirection.Input, newTblAddressBook.UserTelephone2);
			AddParameterToSqlCmd(sqlCmd, "@UserEmailAddress",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserEmailAddress);
			AddParameterToSqlCmd(sqlCmd, "@UserAddressLine1",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserAddressLine1);
			AddParameterToSqlCmd(sqlCmd, "@UserAddressLine2",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserAddressLine2);
			AddParameterToSqlCmd(sqlCmd, "@CompanyName",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.CompanyName);
			AddParameterToSqlCmd(sqlCmd, "@UserCity",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserCity);
			AddParameterToSqlCmd(sqlCmd, "@UserState",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserState);
			AddParameterToSqlCmd(sqlCmd, "@UserZip",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserZip);
			AddParameterToSqlCmd(sqlCmd, "@UserCountry",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserCountry);
			AddParameterToSqlCmd(sqlCmd, "@IsDefault",SqlDbType.Bit, 0, ParameterDirection.Input, newTblAddressBook.IsDefault);
			
			SetCommandType(sqlCmd,CommandType.StoredProcedure,SPR_TBLADDRESSBOOK_CREATENEWTBLADDRESSBOOK);
			ExecuteScalarCmd(sqlCmd);
			return (Int32.Parse(sqlCmd.Parameters["@ReturnValue"].Value.ToString()));

		}
		/// <summary>
        /// Updates an TblAddressBook instance to the database.
        /// </summary>
		public static bool UpdateTblAddressBook(TblAddressBook newTblAddressBook)
		{
			// validate Parameters
			if (newTblAddressBook == null)
				throw (new ArgumentNullException("newTblAddressBook"));

			// Execute SQL Command
			SqlCommand sqlCmd = new SqlCommand();
			int length = 0;
			AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            
			AddParameterToSqlCmd(sqlCmd, "@AddressBookId",SqlDbType.Int, 0, ParameterDirection.Input, newTblAddressBook.AddressBookId);
			AddParameterToSqlCmd(sqlCmd, "@UserId",SqlDbType.Int, 0, ParameterDirection.Input, newTblAddressBook.UserId);
			AddParameterToSqlCmd(sqlCmd, "@UserFirstName",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserFirstName);
			AddParameterToSqlCmd(sqlCmd, "@UserLastName",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserLastName);
			AddParameterToSqlCmd(sqlCmd, "@UserTelephone1",SqlDbType.NVarChar, 50, ParameterDirection.Input, newTblAddressBook.UserTelephone1);
			AddParameterToSqlCmd(sqlCmd, "@UserTelephone2",SqlDbType.NVarChar, 50, ParameterDirection.Input, newTblAddressBook.UserTelephone2);
			AddParameterToSqlCmd(sqlCmd, "@UserEmailAddress",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserEmailAddress);
			AddParameterToSqlCmd(sqlCmd, "@UserAddressLine1",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserAddressLine1);
			AddParameterToSqlCmd(sqlCmd, "@UserAddressLine2",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserAddressLine2);
			AddParameterToSqlCmd(sqlCmd, "@CompanyName",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.CompanyName);
			AddParameterToSqlCmd(sqlCmd, "@UserCity",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserCity);
			AddParameterToSqlCmd(sqlCmd, "@UserState",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserState);
			AddParameterToSqlCmd(sqlCmd, "@UserZip",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserZip);
			AddParameterToSqlCmd(sqlCmd, "@UserCountry",SqlDbType.NVarChar, 500, ParameterDirection.Input, newTblAddressBook.UserCountry);
			AddParameterToSqlCmd(sqlCmd, "@IsDefault",SqlDbType.Bit, 0, ParameterDirection.Input, newTblAddressBook.IsDefault);
			
			SetCommandType(sqlCmd,CommandType.StoredProcedure,SPR_TBLADDRESSBOOK_UPDATETBLADDRESSBOOK);
            ExecuteScalarCmd(sqlCmd);
			int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
			return (returnValue == 0 ? false : true);
		}
		#endregion
		
		#region TblAddressBook Related Collections
		/// <summary>
        /// Creates and returns a strongly typed collection of Employee custom entity. 
        /// The colelction is created through iterating on the IdataReader object which contains Employee information, as a set of records, similar to tabular format.
        /// </summary>
		public static CollectionBase  GenerateTblAddressBookCollectionFromReader(IDataReader returnData) 
		{
			
			//creating the instance of Employee collection
			CustomCollection<TblAddressBook> colTblAddressBook = new CustomCollection<TblAddressBook>();
			
			/************************* Architecure note:**********************************
             * Below code includes the null value functionality to retieve the data which has nill value in database end.
             * Design consideration:
             * Besides general data fields, special care should be taken for primary keys, to assign '0'/defaultvalue, rather passing 'Null' value in constructor parameter.
             * Although we are considering sqldb tyoe data for the current data container, but while retireving data from database end, through datareader object, we need to cast data reader objects using .net primitive data type, 
             * rather using sqldb type to cast, since data reader objects don't support sql db type to be casted.
             *****************************************************************************/

            //Iterating thriugh the data reader, to generate Employee collection.

			//getting the Employee collection by iterating thru the data reader
			while (returnData.Read()) 
			{
				//passing the TblAddressBook constructor parameters from the current instance of data reader fields.
				TblAddressBook newTblAddressBook = new TblAddressBook
				(
					returnData["AddressBookId"]== System.DBNull.Value ? GetAddressBookIdMinValue : (Int32 ) returnData["AddressBookId"],
					returnData["UserId"]== System.DBNull.Value ? 0: (Int32 ) returnData["UserId"],
					returnData["UserFirstName"]== System.DBNull.Value ? string.Empty: (String ) returnData["UserFirstName"],
					returnData["UserLastName"]== System.DBNull.Value ? string.Empty: (String ) returnData["UserLastName"],
					returnData["UserTelephone1"]== System.DBNull.Value ? string.Empty: (String ) returnData["UserTelephone1"],
					returnData["UserTelephone2"]== System.DBNull.Value ? string.Empty: (String ) returnData["UserTelephone2"],
					returnData["UserEmailAddress"]== System.DBNull.Value ? string.Empty: (String ) returnData["UserEmailAddress"],
					returnData["UserAddressLine1"]== System.DBNull.Value ? string.Empty: (String ) returnData["UserAddressLine1"],
					returnData["UserAddressLine2"]== System.DBNull.Value ? string.Empty: (String ) returnData["UserAddressLine2"],
					returnData["CompanyName"]== System.DBNull.Value ? string.Empty: (String ) returnData["CompanyName"],
					returnData["UserCity"]== System.DBNull.Value ? string.Empty: (String ) returnData["UserCity"],
					returnData["UserState"]== System.DBNull.Value ? string.Empty: (String ) returnData["UserState"],
					returnData["UserZip"]== System.DBNull.Value ? string.Empty: (String ) returnData["UserZip"],
					returnData["UserCountry"]== System.DBNull.Value ? string.Empty: (String ) returnData["UserCountry"],
					returnData["IsDefault"]== System.DBNull.Value ? false: (Boolean ) returnData["IsDefault"]

				);
				//adding the Employee to the collection
				colTblAddressBook.Add(newTblAddressBook);
			}
			
			//returns the collection of Companys 
			return (colTblAddressBook);
		}
		
		#endregion
		#region Constants and Default Values
		
		public static Int32 GetAddressBookIdMinValue
		{
			get
			{
				return 0;
			}
		}
		public static Int32 GetUserIdMinValue
		{
			get
			{
				return 0;
			}
		}
		
		//create method related contants
			internal const string SPR_TBLADDRESSBOOK_CREATENEWTBLADDRESSBOOK="spr_TblAddressBook_CreateNewTblAddressBook";
			internal const string SPR_TBLADDRESSBOOK_UPDATETBLADDRESSBOOK="spr_TblAddressBook_UpdateTblAddressBook";
			internal const string SPR_TBLADDRESSBOOK_DELETETBLADDRESSBOOK="spr_TblAddressBook_DeleteTblAddressBook";
			internal const string SPR_TBLADDRESSBOOK_DELETETBLADDRESSBOOKLIST="spr_TblAddressBook_DeleteTblAddressBookList";
			internal const string SPR_TBLADDRESSBOOK_GETTBLADDRESSBOOKBYADDRESSBOOKID="spr_TblAddressBook_GetTblAddressBookByAddressBookId";
			internal const string SPR_TBLADDRESSBOOK_GETTBLADDRESSBOOKBYUSERID="spr_TblAddressBook_GetTblAddressBookByUserId";
			internal const string SPR_TBLADDRESSBOOK_GETTBLADDRESSBOOKBYUSERIDPAGED="spr_TblAddressBook_GetTblAddressBookByUserIdPaged";
			internal const string SPR_TBLADDRESSBOOK_GETTBLADDRESSBOOKBYUSERIDPAGEDCOUNT="spr_TblAddressBook_GetTblAddressBookByUserIdPagedCount";
			internal const string SPR_TBLADDRESSBOOK_GETALLTBLADDRESSBOOK="spr_TblAddressBook_GetAllTblAddressBook";
			internal const string SPR_TBLADDRESSBOOK_GETALLTBLADDRESSBOOKPAGED="spr_TblAddressBook_GetAllTblAddressBookPaged";
			internal const string SPR_TBLADDRESSBOOK_GETALLTBLADDRESSBOOKPAGEDCOUNT="spr_TblAddressBook_GetAllTblAddressBookPagedCount";
		
		#endregion
	}
}	
